package testPackage;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

/**
 * step by step 05 在Selenium 中打开一个新的标签
 * 使用Robot类打开标签页
 * Robot类用于模拟键盘和鼠标事件
 * 执行过程：
 * 1、模拟按下ctrl+ t按键，打开新选项
 * 2、将焦点切换至新选项卡  getWindowHandles()
 */
public class SeleniumOther04 {
    @Test
    public void waitTest() throws AWTException,InterruptedException {
        String str = "D:\\\\Enviroments\\\\geckodriver-v0.26.0-win64\\\\chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", str);
        WebDriver driver = new ChromeDriver();
        driver.get("https://tb.alicdn.com/snapshot/index.html");
        //模拟按下ctrl+t按键
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_T);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyRelease(KeyEvent.VK_T);
        //切换至新选项卡
        ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
        driver.switchTo().window(tabs.get(0));
        //在新选项卡中打开网页
        driver.get("https://tb.alicdn.com/snapshot/index.html");
        Thread.sleep(10000);
        driver.close();
        Thread.sleep(10000);
        driver.quit();
    }
}
